var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        isIPX : app.globalData.isIPX,
        rotate : 0,
        anim : 5,
        myprize : {},
        txtArr: {
            btn_name: "免费抽奖",
            btn_pay: "",
            btn_num: "剩余2次免费机会",
            btn_able: true,
            btn_val: ""
        },
        bgmusic : false,
        closebg : false,
        params : {},
        pparams : {},
        isauth : true,
		shopid : 0,
    },
    onUnload(){
        if( this.data.bgmusic ){
            this.replaybg(2);
        }
    },
    onShow(){
		var self = this;
        if( self.data.pparams.music && !self.data.closebg ){
            this.replaybg(1);
        }
    },
    onHide(){
        if( this.data.bgmusic ){
            this.replaybg(2);
        }
    },
    onLoad:function(options){
        var self = this;
        if( options.scene ){
            var list = options.scene.split('_');
            options.shopid = list[0] ? list[0] : 0;
            options.zfid = list[1] ? list[1] : 0;
        }
        var zfid = options.zfid ? options.zfid : 0;
        var shopid = options.shopid ? options.shopid : 0;
        self.data.options = options;
        
        var postdata = {
            op : 'info',
            zfid : zfid,
            shopid : shopid,
        }
        
        app.com.auth(function(user){
            app.com.http('zhuanpan','POST',postdata,0,false,'','',function(res){
                if (res.data && !res.data.errno) {

                    self.setData({
                        inited : true,
                        info : res.data.data.info,
                    })
                    setTimeout(function(){
                        self.setData({
                            isauth : res.data.data.isauth ? res.data.data.isauth : 0,
                            prizes : res.data.data.prize,
                            params : res.data.data.params,
                            pparams : res.data.data.pparams,
                            logarr : res.data.data.logarr,
                            sharedata : res.data.data.sharedata,
                            shopid : postdata.shopid,
                        });

                        if( res.data.data.pparams.music ) {
                            self.playbg();
                        }

                        // 关闭下拉刷新
                        wx.stopPullDownRefresh();
                    },5)
                    
                }else{
                    app.com.alert(res.data.message);
                }

                wx.setNavigationBarTitle({
                    title:  '每天抽奖',
                });
                
            });
        });
       
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
        }
    },
    showRule(){
        this.setData({
            showrule : !this.data.showrule
        })
    },
    getLottery(){
        var self = this;
        if( self.data.ising ) return false;

        if( self.data.isauth ){
            this.loginauth = this.selectComponent('#loginauth');
            this.loginauth.hideauth();
            return false;
        }

        var postdata = {
            op:'getprize',
            shopid : self.data.shopid,
        }
        app.com.getLocation(self,function(loc){
            postdata.lng = loc.longitude;
            postdata.lat = loc.latitude;

            self.data.ising = true;
            app.com.http('zhuanpan','POST',postdata,0,true,'','',function(res){
                if (res.data && !res.data.errno && res.data.data.prize) {

                    var id = 1;
                    for (var i = 0; i < self.data.prizes.length; i++) {
                        if( self.data.prizes[i].id == res.data.data.prize.id || (res.data.data.prize.id == -1 && self.data.prizes[i].type == 0) ){
                            self.setData({
                                myprize : res.data.data.prize,
                                anim : 5,
                                isAnimate : true
                            })
                            id = i+1;
                        }
                    }

                    self.lottery(id, res.data.data.prize.name, 1, 'aaa');
                }else{
                    app.com.alert(res.data.message);
                    self.data.ising = false;
                }
            });

        })
    },
    lottery: function(index, prizename, isprize, n) {
        var self = this;

        var s = this.data.rotate;
        0 == s && (s = 22.5);
        s += 3600 - 45 * index;
        this.setData({
            rotate: s
        });
        this.data.rotate -= -45 * index;
        setTimeout(function() {
            if( isprize == 1 ){
                self.setData({
                    showprize: true,
                    showani: true,
                    showprizetxt: prizename,
                    isAnimate: false,
                    ising : false,
                    rotate : 0,
                    anim : 0,
                    'pparams.last' : self.data.pparams.last - 1,
                });
            }else if(isprize == 0){
                self.setData({
                    isAnimate: false,
                    ising : false,
                    rotate : 0,
                    anim : 0,
                    'pparams.last' : self.data.pparams.last - 1,
                });
            }
        }, 5000);
    },
    hidep(){
        var self = this;
        self.setData({
            showani: false
        }, function() {
            setTimeout(function() {
                self.setData({
                    showprize: false
                });
            }, 500);
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        if( this.data.bgmusic ){
            this.data.bgmusic.destroy();
        }
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    playbg(){
        var self = this;
        if( self.data.bgmusic ){
            self.data.bgmusic.destroy();
        }
        self.data.bgmusic = wx.createInnerAudioContext();
        self.data.bgmusic.autoplay = true;
        self.data.bgmusic.loop = true;
        self.data.bgmusic.src = self.data.pparams.music;
        self.data.bgmusic.play();
        self.setData({
            audioplaying : true
        });
    },
    replaybg(type){
        var self = this;
        if( type == 1 ){
            self.setData({
                audioplaying : true,
            });
            self.data.bgmusic.play();
        }else if(type == 2){
            self.setData({
                audioplaying : false,
            });
            self.data.bgmusic.pause();
        }else{
            self.setData({
                audioplaying : !self.data.audioplaying,
                closebg : !self.data.closebg,
            });
            if( self.data.audioplaying ){
                self.data.bgmusic.play();
            }else{
                self.data.bgmusic.pause();
            }
        }
    },
    authed(){
        this.setData({
            isauth : false,
        })
        this.getLottery();
    }
})